package im.yisme.view.ui.animation;

import java.util.ArrayList;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.PointF;
import android.util.Log;

import im.yisme.R;
import im.yisme.view.ui.animation.pojo.LineBitmapPOJO;
import im.yisme.view.ui.animation.pojo.LinePOJO;
import im.yisme.view.ui.util.UIparams;

public class Comet extends Thread{

	private long endTime;
	private LineBitmapPOJO comet;
	private ArrayList<LineBitmapPOJO> list;
	private ArrayList<Comet> threadList;
	private boolean pause;
	private static final int ANIMATION_UPDATE_INTERVAL = 10;
	private static final int PAUSE_POLLING_INTERVAL = 500;
	private static final int ALPHA_CHANGING_DURATION = 200; 
	
	
	private Bitmap line1,line2,line1_reverse,line2_reverse;
	
	public Comet(long endTime,int[] points,ArrayList<LineBitmapPOJO> cometArrayList,ArrayList<Comet> cometThreadArrayList,Resources res){
	     this.endTime=endTime;
	     
	     
	     comet=new LineBitmapPOJO();
	     
	     initLines(res);
         init(points);	

	     list=cometArrayList;
	     threadList=cometThreadArrayList;
	     list.add(comet);
	     threadList.add(this);
	     pause=false;
	     
	     
	}
    public void run(){
    	
    	try {
			
    	long startTime = System.currentTimeMillis();
    	int duration = (int) (endTime - startTime);
    		
    	while(System.currentTimeMillis()<endTime){
    		
    		while(pause) Thread.sleep(PAUSE_POLLING_INTERVAL);
    		
    		
    		if(duration > ALPHA_CHANGING_DURATION){
    			
    			if(System.currentTimeMillis() - startTime < ALPHA_CHANGING_DURATION){
    				int alpha = (int) (255 * (System.currentTimeMillis()-startTime)/ALPHA_CHANGING_DURATION);
        			comet.setAlpha(alpha);
    			}
    			else if(endTime - System.currentTimeMillis() < ALPHA_CHANGING_DURATION){
    				int alpha = (int) (255 * (endTime - System.currentTimeMillis())/ALPHA_CHANGING_DURATION);
        			comet.setAlpha(alpha);
    			}
    			else {
					comet.setAlpha(255);
				}
    			
    		}
    		else {
    			comet.setAlpha(255);
			}

    		
    		
    		Thread.sleep(ANIMATION_UPDATE_INTERVAL);
    	}
    	
    	} catch (Exception e) {
			// TODO: handle exception
    		e.printStackTrace();
		}
    	
    	list.remove(comet);
    	threadList.remove(this);
    } 

    public void pauseRunning(){
		pause=true;
	}
	public void resumeRunning(long time){
		pause=false;
		endTime=endTime+time;
	}
	public void living(){
		
	}
	public void dead(){
		
	}
	
	private void init(int[] pointsArr) {
		// TODO Auto-generated method stub
	    int index = 0;
	    ArrayList<Bitmap> lines = new ArrayList<Bitmap>();
	    ArrayList<PointF> points = new ArrayList<PointF>();
	    while(index<pointsArr.length-1){
	    	int sum = pointsArr[index]+pointsArr[index+1];
	    	int oriPre = (int) Math.sqrt(pointsArr[index]) - 1;
	    	int oriNext = (int) Math.sqrt(pointsArr[index+1]) - 1;
	    	int min;
	    	boolean sTob;
	    	
	    	if(oriPre > oriNext){
	    		min = oriNext;
	    		sTob = false;
	    	}
	    	else {
				min = oriPre;
				sTob = true;
			}
	    	
	    	switch (sum) {
			case 5:
				if(sTob)
					lines.add(line1);
				else 
					lines.add(line1_reverse);	
				
				points.add(new PointF(UIparams.center[min][0], UIparams.center[min][1]-line1.getHeight()/2));
				break;
			case 10:
				if(sTob)
					lines.add(line2);
				else 
					lines.add(line2_reverse);	
				
				points.add(new PointF(UIparams.center[min][0] - line2.getWidth()/2, UIparams.center[min][1]));
				break;
			case 20:
				if(sTob)
					lines.add(line2);
				else 
					lines.add(line2_reverse);
				
				points.add(new PointF(UIparams.center[min][0] - line2.getWidth()/2, UIparams.center[min][1] ));
				break;
				
			case 25:
				if(sTob)
					lines.add(line1);
				else 
					lines.add(line1_reverse);
				
				points.add(new PointF(UIparams.center[min][0], UIparams.center[min][1]-line1.getHeight()/2));
				break;
			
			default:
				break;
			}
	    	
	    	index++;
	    }
	     
	    comet.setLines(lines);
	    comet.setPoints(points);
	}
	private void initLines(Resources res){
		line1 = BitmapFactory.decodeResource(res, R.drawable.line1);
		line2 = BitmapFactory.decodeResource(res, R.drawable.line2);
		
		line1_reverse = BitmapFactory.decodeResource(res, R.drawable.line1_reverse);
		line2_reverse = BitmapFactory.decodeResource(res, R.drawable.line2_reverse);
	}
}
